<template>
  <div>
    <!-- 添加弹出框 -->
    <el-dialog
      :title="dialogFormTitle"
      :visible.sync="dialogFormVisible"
      @close="closedialog"
    >
      <el-form
        ref="produceForm"
        :rules="rules"
        :model="productionovermodify"
        label-width="80px"
        style="width: 300px"
      >

      <el-form-item label="产品名称">
        {{ productionovermodify.order.pname }}
      </el-form-item>

      <el-form-item label="创建时间">
        {{ productionovermodify.order.stime }}
      </el-form-item>

      <el-form-item label="生产时间">
        {{ productionovermodify.stime }}
      </el-form-item>

      <el-form-item label="预计结束">
        {{ productionovermodify.etime }}
      </el-form-item>

      <el-form-item label="实际结束">
        {{ productionovermodify.order.ertime }}
      </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="dialogFormVisible = false">确定</el-button>
      </div>
    </el-dialog>

    <!-- 搜索表单 -->
    <el-form
      ref="productionproduced"
      :model="productionproduced"
      label-width="80px"
      :inline="true"
    >
      <el-form-item>
        <el-button type="primary" @click="batchDelete">批量删除</el-button>
      </el-form-item>

      <el-form-item label="生产单号">
        <el-input v-model="productionproduced.order.poid" clearable></el-input>
      </el-form-item>

      <el-form-item label="产品名称">
        <el-input v-model="productionproduced.order.pname" clearable></el-input>
      </el-form-item>

      <el-form-item label="生产员工">
        <el-input
          v-model="productionproduced.employee.ename"
          clearable
        ></el-input>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="onSubmit">查询所有信息</el-button>
        <el-button @click="clearAllFields">清空</el-button>
      </el-form-item>
    </el-form>

    <!-- 展示数据 -->
    <el-table
      :data="pageBean.beanList"
      style="width: 100%"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55"> </el-table-column>
      <el-table-column prop="order.poid" label="生产单号" width="140" align="center">
      </el-table-column>
      <el-table-column prop="order.pname" label="产品名称" width="100" align="center">
      </el-table-column>
      <el-table-column prop="order.yield" label="计划产量" width="100" align="center">
      </el-table-column>
      <el-table-column prop="order.ryield" label="实际产量" width="100" align="center">
      </el-table-column>
      <el-table-column prop="order.rate" label="完工率" width="100" align="center">
      </el-table-column>
      <el-table-column prop="order.days" label="生产天数" width="100" align="center">
      </el-table-column>
      <el-table-column prop="employee.ename" label="生产员工" width="100" align="center">
      </el-table-column>
      <el-table-column prop="order.efficiency" label="生产效率" width="200" align="center">
      </el-table-column>

      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button size="mini" @click="handEdit(scope.$index, scope.row)"
            >查看详情</el-button
          >
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.$index, scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <!-- 展示页码 -->
    <div class="block">
      <span class="demonstration"></span>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageIndex"
        :page-sizes="[1, 3, 6]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="pageBean.totalRecord"
      >
      </el-pagination>
    </div>
  </div>
</template>
  
  <script>
import axios from "axios";

export default {
  data() {
    var checkenum = (rule, value, callback) => {
      if (value === undefined || value === "") {
        callback(new Error("填写数量"));
      } else if (!/^[1-9]\d*$/.test(value)) {
        callback(new Error("数量格式错误"));
      }else {
        callback();
      }
    };
    return {
      isEdit: false,
      produceForm: null,
      dialogFormVisible: false,
      multipleSelection: [],
      dialogFormTitle: "",
      pageIndex: 1,
      pageSize: 6,
      pageBean: {},
      goods: {},
      employees:{},
      rules: {
        'order.punit': [
          { required: true, message: "请选择产品单位", trigger: "change" },
        ],
        'order.prnum': [
          { required: true, message: "请输入产品数量", trigger: "blur" },
          { validate: checkenum, trigger: "blur" },
        ],
        'order.time': [
          {
            required: true,
            message: "请选择日期",
            trigger: "change",
          },
        ],
        'employee.ename': [
          {
            required: true,
            message: "请选择员工",
            trigger: "change",
          },
        ],
        

      },
      productionproduced: {
        order: {
          id: null,
          poid: null,
          pname: null,
          pnum:null,
          punit:null,
          prnum:null,
          prunit:null,
          rate:null,
          storage:null,
          efficiency:null,
          days:null
        },
        employee: {
          id: null,
          ename: null,
        },
      },
      productionovermodify:{
        order: {
          id: null,
          poid: null,
          pname: null,
          pnum:null,
          punit:null,
          prnum:null,
          prunit:null,
          rate:null,
          storage:null,
          efficiency:null,
          days:null,
          time:null,
          stime:null,
          ertime:null
        },
        employee: {
          id: null,
          ename: null,
        },
      }

    };
  },
  mounted() {
    this.getAll();
    console.log(new Date())
  },
  methods: {
    //  获取当前时间
    currentDate() {
      return (
        new Date().getFullYear() +
        "-" +
        ("0" + (new Date().getMonth() + 1)).slice(-2) +
        "-" +
        ("0" + new Date().getDate()).slice(-2)
      );
    },
    // 查询所有生产员工
    getAllEmployee() {
      axios.post("/produced/findAllEmployee").then((resp) => {
        this.employees = resp.data.data;
      });
    },
    // 查询所有信息
    getAll() {
      axios
        .post(
          "/produced/findAllProducedOver/" + this.pageIndex + "/" + this.pageSize,
          this.productionproduced
        )
        .then((resp) => {
          if (resp.data.code == 200) {
            this.pageBean = resp.data.data;
          }
        });
    },
    //  查询按钮
    onSubmit() {
      this.pageIndex = 1;
      this.pageSize = 6;
      this.getAll();
    },
    //  一键清空
    clearAllFields() {
      this.productionproduced={
        order: {
          id: null,
          poid: null,
          pname: null,
          pnum:null,
          punit:null,
          prnum:null,
          prunit:null,
          rate:null,
          storage:null,
          efficiency:null,
          days:null
        },
        employee: {
          id: null,
          ename: null,
        },
      }
      this.getAll();
    },
    //  添加弹出框
    handAdd() {
      (this.dialogFormVisible = true), 
      (this.dialogFormTitle = "申请入库")
    },
    //  查看详情
    handEdit(index, row) {
      this.dialogFormVisible = true,
      this.dialogFormTitle = "详细情况",
      this.productionovermodify = { ...row }
    },
    //  关闭弹出框
    closedialog() {
      //  情况数据
      this.clearAllFields();
    },
    //  复选框选中
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    // 分页
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      //  重新查询
      this.pageSize = val;
      this.getAll();
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      //  重新查询
      this.pageIndex = val;
      this.getAll();
    },
    //  删除
    handleDelete(index, row) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          axios.post("/produced/deleteP", row).then((resp) => {
            if (resp.data.code == 200) {
              this.$message("删除成功");
              this.getAll();
            } else {
              this.$message.error("删除失败");
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    //  批量删除
    batchDelete(index, row) {
      if (this.multipleSelection.length > 0) {
        let myids = [];
        for (let i = 0; i < this.multipleSelection.length; i++) {
          myids.push(this.multipleSelection[i].id);
        }
        this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        })
          .then(() => {
            axios
              .post("/produced/deletebatchgetP", { ids: myids })
              .then((resp) => {
                if (resp.data.code == 200) {
                  this.$message("删除成功");
                  this.getAll();
                } else {
                  this.$message.error("删除失败");
                }
              });
          })
          .catch(() => {
            this.$message({
              type: "info",
              message: "已取消删除",
            });
          });
      } else {
        this.$message.error("请选择要删除的数据");
      }
    },
  },
};
</script>
  
  <style>
</style>